package com.authority.framework.web.controller;

import com.authority.framework.online.dao.OnlineSessionDAO;
import com.authority.framework.log.Log;
import com.authority.framework.log.enums.BusinessType;
import com.authority.framework.online.OnlineSession;
import com.authority.framework.web.domain.SysUserOnlineEntity;
import com.authority.framework.web.service.SysUserOnlineService;
import com.authority.framework.base.util.ShiroUtils;
import com.authority.framework.base.util.laydata.LayDatas;
import com.authority.framework.base.util.laydata.PageUtils;
import com.authority.framework.base.util.laydata.ResultModel;
import com.authority.framework.base.util.laydata.ResultTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: authority
 * @description: 登录日志控制层
 * @author: Mr.Yan
 * @create: 2018-11-20 18:01
 **/
@Controller
@RequestMapping(value = "sysuseronlineCon")
public class SysUserOnlineController {

	private static final Logger log = LoggerFactory.getLogger(SysUserOnlineController.class);

	@Autowired
	private SysUserOnlineService sysUserOnlineService;

	@Autowired
	private OnlineSessionDAO onlineSessionDAO;

	private Map<String, Object> paramMap;//参数

	private Map<String, Object> resultMap;//返回值

	/**
	 * @Description: 获取在线用户展示数据
	 * @Param:
	 * @return:
	 * @Author: Mr.Yan
	 * @Date: 2018/12/15
	 */
	@RequestMapping("/list")
	@ResponseBody
	public LayDatas list(HttpServletRequest request, HttpServletResponse response,
			PageUtils pageUtils) {
		//剔除自己
		String sessionId = String.valueOf(ShiroUtils.getSession().getId());
		paramMap = new HashMap<>();
		paramMap = (Map)pageUtils.getParas();
		paramMap.put("sessionId",sessionId);
		pageUtils.setParas(paramMap);
		return sysUserOnlineService.pageLayInfo("system/SysUserOnlineSql", SysUserOnlineEntity.class, pageUtils);
	}

	@Log(title = "在线用户管理", businessType = BusinessType.FORCE)
	@ResponseBody
	@RequestMapping(value = "/forceLogout")
	public ResultModel forceLogout(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(name = "sessionIds", required = false) String sessionIds) {
		try {
			String[] sessionIdArr = sessionIds.split(",");
			for (String sessionId : sessionIdArr) {
				OnlineSession onlineSession = (OnlineSession)onlineSessionDAO.readSession(sessionId);
				//异地登录
				onlineSession.setStatus(OnlineSession.OnlineStatus.off_line);
				sysUserOnlineService.delete(SysUserOnlineEntity.class,onlineSession.getId());
			}
			return ResultTools.result(0, "", null);
		}
		catch (Exception e) {
			log.error("强退在线信息出现未知错误" + e.getMessage());
			return ResultTools.result(404, "强退在线信息出现未知错误", null);
		}
	}

}
